/* eslint-disable eqeqeq */
import { Message, Notification } from 'element-ui'

var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOlsiaWZhYnUiLCJjNjM5ZWVmZC01MWU4LTRjZjYtOTdmZC05ZTQ5N2UwY2RhNGEiXSwiZXhwIjoxNzY3NDIxMjQ5fQ.DTPgi9IfGhmgsM5yK-Jq6TjGMyaZsxtQ0llMfPO1dhM'
var ws
var tt
var lockReconnect = false// 避免重复连接
var clientId = localStorage.getItem('clientId')// 缓存中取出客户端id

var websocket = {
    Init: function (url, device, closeAction) {
        return new Promise((resolve, reject) => {
            if ('WebSocket' in window || 'MozWebSocket' in window) {
                ws = new WebSocket(url + token)
            } else {
                console.log('您的浏览器不支持 WebSocket!')
                return
            }
            ws.onmessage = function (e) {
                console.log('接收消息:' + e.data)
                let data = JSON.parse(e.data)
                // heartCheck.start()
                if (data.msg == 'installFinish') { 
                    if (data.status == 'fail') { 
                        Notification({
                            title: '通知',
                            message: device.chiName + '安装失败',
                            duration: 0
                        })
                    } else if (data.status == 'success') {
                        Notification({
                            title: '通知',
                            message: device.chiName + '安装成功',
                            duration: 0
                        })
                    }
                    if (closeAction == 'install') {
                        ws.close()
                    }
                   
                }
                if (data.msg == 'step' && data.des == 'FastBot log') {
                    Notification({
                        title: '通知',
                        message: device.chiName + 'fast命令推送成功，正在执行',
                        duration: 0
                    })
                    ws.close()
                }
                
                messageHandle(e.data)
                resolve(e.data)
            }
    
            ws.onclose = function () {
                console.log('连接已关闭')
               
            }
    
            ws.onopen = function () {
                console.log('连接成功')
                Message({
                    message: device.chiName + '连接成功',
                    type: 'success'
                })
                // heartCheck.start()
            }
    
            ws.onerror = function (e) {
                console.log('数据传输发生错误')
                Message({
                    message: '数据传输发生错误',
                    type: 'error'
                })
                messageHandle(Message)
                reconnect(clientId)
                // reject(err)
            }
        })

    },
    Send: function (data) {
        let msg = JSON.stringify(data)
        console.log('发送消息：' + msg)
        ws.send(msg)
    },
    getWebSocket() {
        return ws
    },
    getStatus() {
        if (ws.readyState == 0) {
            return '未连接'
        } else if (ws.readyState == 1) {
            return '已连接'
        } else if (ws.readyState == 2) {
            return '连接正在关闭'
        } else if (ws.readyState == 3) {
            return '连接已关闭'
        }
    },
    close() {
        ws.close()
    }
}

export default websocket

// 根据消息标识做不同的处理
function messageHandle(message) {
    let msg = JSON.parse(message)
    switch (msg.flag) {
        case 'command':
            console.log('指令消息类型')
            break
        case 'inform':
            console.log('通知')
            break
        default:
            console.log('未知消息类型')
    }
}

function reconnect(sname) {
    if (lockReconnect) {
        return
    };
    lockReconnect = true
    // 没连接上会一直重连，设置延迟避免请求过多
    tt && clearTimeout(tt)
    tt = setTimeout(function () {
        console.log('执行断线重连...')
        websocket.Init(sname)
        lockReconnect = false
    }, 4000)
}



// 心跳检测
// var heartCheck = {
//     timeout: 1000 * 60 * 3,
//     timeoutObj: null,
//     serverTimeoutObj: null,
//     start: function () {
//         console.log('开始心跳检测')
//         var self = this
//         this.timeoutObj && clearTimeout(this.timeoutObj)
//         this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj)
//         this.timeoutObj = setTimeout(function () {
//             // 这里发送一个心跳，后端收到后，返回一个心跳消息，
//             // onmessage拿到返回的心跳就说明连接正常
//             console.log('心跳检测...')
//             ws.send('HeartBeat:' + clientId)
//             self.serverTimeoutObj = setTimeout(function () {
//                 if (ws.readyState != 1) {
//                     ws.close()
//                 }
//                 // createWebSocket();
//             }, self.timeout)

//         }, this.timeout)
//     }
// }

